using System;
using System.Drawing;
using System.Windows.Forms;

namespace codebase.UI
{
    public class Renderer_GDI : Renderer
    {
        public Bitmap backbuffer;
        public Font font = new Font("Consolas", 8);
        public Graphics g;

        public Renderer_GDI()
        {
            backbuffer = new Bitmap(240, 320);
            g = Graphics.FromImage(backbuffer);
        }

        public override int Width
        {
            set { backbuffer = new Bitmap(value, Height); g = Graphics.FromImage(backbuffer); }
            get { return backbuffer.Width; }
        }

        public override int Height
        {
            set { backbuffer = new Bitmap(Width, value); g = Graphics.FromImage(backbuffer); }
            get { return backbuffer.Height; }
        }

        public override int CharacterWidth
        {
            get { return 7; }
        }

        public override int CharacterHeight
        {
            get { return 8; }
        }

        public override void Clear()
        {
            g.Clear(Color.White);
        }

        Color toColor(int color)
        {
            return Color.FromArgb(255, Color.FromArgb(color));
        }

        public override void DrawLine(int x1, int y1, int x2, int y2, int color)
        {
            g.DrawLine(new Pen(toColor(color)), x1, y1, x2, y2);
        }

        public override void DrawRectangle(int x1, int y1, int x2, int y2, int color)
        {
            g.DrawRectangle(new Pen(toColor(color)), x1, y1, x2 - x1 -1, y2 - y1 -1);
        }

        public override void DrawFilledRectangle(int x1, int y1, int x2, int y2, int color)
        {
            g.FillRectangle(new SolidBrush(toColor(color)), x1, y1, x2 - x1 -1, y2 - y1 -1);
        }

        public override void DrawCircle(int x, int y, int radius, int color)
        {
            g.DrawEllipse(new Pen(toColor(color)), x, y, radius, radius);
        }

        public override void DrawFilledCircle(int x, int y, int radius, int color)
        {
            g.FillEllipse(new SolidBrush(toColor(color)), x, y, radius, radius);
        }

        public override void DrawText(string text, int x, int y, bool transparent, int backcolor, int forecolor)
        {
            SizeF size = g.MeasureString(text,font);
            if (!transparent) DrawFilledRectangle(x, y, x + (int)size.Width, y + (int)size.Height, backcolor);
            g.DrawString(text, font, new SolidBrush(toColor(forecolor)), x, y);
        }
    }
}
